<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { queryLeft1 } from '@/api/business/largeScreen'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null,
      dataAll: [389, 259, 262, 324, 232, 176, 196, 214, 133, 370],
      yAxisData: ['原因1', '原因2', '原因3', '原因4', '原因5']
    }
  },
  mounted() {
    this.initChart('03')
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(zhlb) {
      this.chart = echarts.init(this.$el, 'macarons')

      queryLeft1({ zhlb: zhlb }).then(res => {
        console.log(res)
        this.chart.setOption({
          title: [
            { text: '每月申请人数', x: '2%', y: '1%', textStyle: { color: '#fff', fontSize: '14' }},
            { text: '每月各区申请人数', x: '40%', y: '1%', textStyle: { color: '#fff', fontSize: '14' }},
            { text: '各级别投诉占比', x: '2%', y: '50%', textStyle: { color: '#fff', fontSize: '14' }}
          ],
          grid: [
            { x: '50%', y: '7%', width: '45%', height: '90%', containLabel: true }
          ],
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          calculable: true,
          xAxis: [
            {
              type: 'value',
              boundaryGap: [0, 0.01],
              axisLabel: {
                textStyle: {
                  color: '#fff',
                  fontSize: 12,
                  textShadowColor: '#000',
                  textShadowOffsetY: 2
                }
              },
              axisLine: {
                lineStyle: {
                  color: '#DDD'
                }
              },
              splitLine: {
                lineStyle: {
                  type: 'dashed',
                  color: '#DDD'
                }
              }
            }
          ],
          yAxis: [
            {
              type: 'category', // y轴
              data: res.timeArray,
              axisLabel: {
                textStyle: {
                  color: '#fff',
                  fontSize: 12,
                  textShadowColor: '#000',
                  textShadowOffsetY: 2
                }
              },
              axisLine: {
                lineStyle: {
                  color: '#DDD'
                }
              }
            }
          ],
          series: [
            {
              name: '南关',
              type: 'bar',
              color: '#86c9f4',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220102
            },
            {
              name: '宽城',
              type: 'bar',
              color: '#4da8ec',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220103
            },
            {
              name: '朝阳',
              type: 'bar',
              color: '#3a91d2',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220104
            },
            {
              name: '二道',
              type: 'bar',
              color: '#005fa6',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220105
            },
            {
              name: '绿园',
              type: 'bar',
              color: '#315f97',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220106
            },
            {
              name: '每月申请人数',
              type: 'pie',
              radius: '30%',
              center: ['22%', '25%'],
              color: ['#86c9f4', '#4da8ec', '#3a91d2', '#3da4f1', '#315f97'],
              data: res.pie1Array,
              tooltip: {
                formatter: '{a} <br/>{b} : {c} ({d}%)'
              }
            },
            {
              name: '各级别投诉占比',
              type: 'pie',
              radius: '30%',
              center: ['22%', '75%'],
              color: ['#86c9f4', '#4da8ec', '#3a91d2', '#005fa6', '#315f97'],
              labelLine: { normal: { show: false }},
              data: [
                { value: 335, name: 'A级' },
                { value: 310, name: 'B级' },
                { value: 234, name: 'C级' },
                { value: 135, name: 'D级' }
              ],
              itemStyle: { normal: { label: { show: true, formatter: '{b} \n ({d}%)', textStyle: { color: '#B1B9D3' }}}}
            }

          ]
        })
      })
    },
    tooltipFormatter(params) {
      var valuesFormatter = []
      if (params.componentSubType === 'pie') {
        valuesFormatter
          .push('<div style="border-bottom: 1px solid rgba(255,255,255,.3); font-size: 18px;padding-bottom: 7px;margin-bottom: 7px">' +
            '申请人数' +
            '<br>' +
            '</div>' +
            '<span>' + params.name + '</span>: ' + params.value)
      } else {
        valuesFormatter
          .push('<div style="border-bottom: 1px solid rgba(255,255,255,.3); font-size: 18px;padding-bottom: 7px;margin-bottom: 7px">' +
            params.seriesName +
            '</div>' +
            '<span>' + params.name + '</span>: ' + params.value + '<br>')
      }

      return valuesFormatter
    }
  }
}
</script>
